package com.haidong.controller.interceptor;

import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class ProjectInterceptor implements HandlerInterceptor {
    @Override
    // 原方法调用前执行的内容,返回值表示是否放行
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // 前两个参数是请求和响应,第三个参数是被调用的处理器方法,本质上是封装的Method方法
        HandlerMethod handlerMethod = (HandlerMethod) handler;
        String methodName = handlerMethod.getMethod().getName();
        System.out.println("方法名是:" + methodName);
        return true;
    }

    @Override
    // 原始方法执行后的
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        // 前三个参数如上,最后一个是处理返回值,但是现在传输数据都是json,作用不大
        System.out.println("方法执行后1");
    }

    @Override
    // 拦截器最后执行的方法,不管原始方法是否执行
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("拦截完成1");
    }
}
